namespace hjc {

	export class EntityManager extends Singleton {

		private $map: Map<string, Entity> = new Map;
		public remove(entity: Entity): void { this.$map.delete(entity.id); }
		public setById(entity: Entity, id: number): void { this.$map[id] = entity; }
		public add(entity: Entity): void {
			if (!entity.id) entity['$id'] = 'sys_' + this.$map.size + 1;
			this.$map.set(entity.id, entity);
		}

		public getById<E extends Entity>(id: string): E {
			return this.$map.get(id) as E;
		}

		public getAllBy<E extends Entity>(clazz?: new () => E): E[] {
			let arr = Array.from(this.$map.values()) as E[];
			if (!!clazz) return arr.filter(entity => entity instanceof clazz);
			else return arr;
		}
	}

}